package org.lanqiao.controller.information;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParams;
import org.lanqiao.config.entity.ResponseData;
import org.lanqiao.information.Answer;
import org.lanqiao.information.Ask;
import org.lanqiao.information.Comment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.lanqiao.information.IAskService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/information/ask")
@Api(tags = "求助帖Ask模块API")
public class AskController {

    @Reference
    private IAskService askService;

    @PostMapping("/add")
    @ApiOperation(value = "新增求助帖", notes = "根据参数新增一个求助帖")
    @ApiImplicitParam(value = "求助帖实体", name = "ask", required = true, paramType = "query", dataTypeClass = Ask.class)
    public ResponseData<Ask> add(@RequestBody Ask ask) {
        try {
            // 新增求助帖
            // 用户所传数据应为： title content userId
            // 后台所设属性应为： 创建时间createTime 求助帖状态status 0待审核 访问量pv 0
            ask.setCreateTime(new Date());
            ask.setStatus(0);
            ask.setViewCount(0);
            askService.add(ask);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }
    @PostMapping("/changeStatus")
    @ApiOperation(value = "改变求助帖状态", notes = "改变某个求助帖状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "askId", value = "求助帖Id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "status", value = "求助帖状态status", required = true, paramType = "query", dataType = "int"),
    })
    public ResponseData<Ask> changeStatus(@RequestParam Integer askId, @RequestParam Integer status) {
        try {
            askService.changeStatus(askId, status);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/logicallyDelete")
    @ApiOperation(value = "逻辑删除求助帖", notes = "根据主键id将求助帖状态变为已删除")
    @ApiImplicitParam(value = "求助帖主键id", name = "id", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Ask> logicallyDelete(@RequestParam Integer id) {
        try {
            // 这里的删除只为逻辑删除 将求助帖的状态status设置为3 被删除
            askService.logicallyDelete(id);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/physicallyDelete")
    @ApiOperation(value = "物理删除求助帖", notes = "根据主键id将求助帖实体删除，无法恢复")
    @ApiImplicitParam(value = "求助帖主键id", name = "id", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Ask> physicallyDelete(@RequestParam Integer id) {
        try {
            // 这里的删除只为逻辑删除 将求助帖的状态status设置为3 被删除
            askService.physicallyDelete(id);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新求助帖", notes = "根据求助帖实体更新某个求助帖")
    @ApiImplicitParam(value = "求助帖实体", name = "ask", required = true, paramType = "query", dataTypeClass = Ask.class)
    public ResponseData<Ask> update(@RequestBody Ask ask) {
        try{
            // 更新了求助帖之后，要设置更新时间
            ask.setUpdateTime(new Date());
            askService.update(ask);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/findAskById")
    @ApiOperation(value = "查询求助帖", notes = "根据求助帖主键id查询某个特定的求助帖")
    @ApiImplicitParam(value = "求助帖主键id", name = "askId", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Ask> findAskById(@RequestParam Integer askId, HttpSession session) {
        try {
            System.out.println(session);
            Ask ask = askService.findAskById(askId);
            return ResponseData.success().putDataVule("ask", ask);
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }


    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有求助帖", notes = "查询所有求助帖")
    public ResponseData<Ask> findAll(HttpSession session) {
        try {
            System.out.println(session);
            List<Ask> askList = askService.findAll();
            return ResponseData.success().putDataVule("askList", askList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

/*

    @PostMapping("/findAll")
    @ApiOperation(value = "排序测试", notes = "查询所有求助帖,按照创建时间升序排列")
    public ResponseData<Ask> findAll(@RequestBody Map<String, String> queryMap) {
        try {
            List<Ask> askList = null;
            if (queryMap == null) {
                askList = askService.findAll();
            } else {
                askList = askService.findAll(queryMap);
            }
            return ResponseData.success().putDataVule("askList", askList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }
*/



    @PostMapping("/findList")
    @ApiOperation(value = "条件查询求助帖",
            notes = "条件查询求助帖，必须有至少一个条件" +
                    "askId、title、content、status、informUserId、informContent、userId" +
                    "   排序使用orderby 例如orderby:create_time、orderby:update_time...")
    @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataType = "Map")
    public ResponseData<Ask> findList(@RequestBody Map<String, String> queryMap) {
        try {
            List<Ask> askList = askService.findList(queryMap);
            return ResponseData.success().putDataVule("askList", askList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }


    @PostMapping("/pageList")
    @ApiOperation(value = "分页条件查询求助帖",
            notes = "分页条件查询求助帖，map中必须有至少一个条件" +
                    "askId、title、content、status、informUserId、informContent、userId" +
                    "   排序使用orderby 例如orderby:create_time、orderby:create_time...")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataType = "Map")
    })
    public ResponseData<Ask> pageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Map<String, String> queryMap) {
        try {
            IPage<Ask> page = askService.pageList(pageNum, pageSize, queryMap);
            return ResponseData.success().putDataVule("askList", page.getRecords()).putDataVule("total", page.getTotal()).putDataVule("pages", page.getPages());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }


    @PostMapping("/pageListByUserId")
    @ApiOperation(value = "分页根据用户查找求助", notes = "分页条件根据用户id查找数据库中的求助")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataType = "map")
    })
    public ResponseData<Answer> pageListByUserId(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Map<String, String> queryMap) {
        try {
            IPage<Ask> page = askService.pageListByUserId(pageNum, pageSize, queryMap);
            return ResponseData.success().putDataVule("answerList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/pageAll")
    @ApiOperation(value = "分页查询所有",
            notes = "分页查询所有求助帖")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class)
    })
    public ResponseData<Ask> findAll(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        try {
            IPage<Ask> page = askService.pageAll(pageNum, pageSize);
            return ResponseData.success().putDataVule("askList", page.getRecords()).putDataVule("total", page.getTotal()).putDataVule("pages", page.getPages());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/inform")
    @ApiOperation(value = "举报求助帖", notes = "举报求助帖，需要求助帖askId，举报人informUserId，举报原因informContent，缺一不可")
    @ApiImplicitParam(name = "informMap", value = "举报条件", required = true, paramType = "body", dataType = "Map")
    public ResponseData<Ask> inform(@RequestBody Map<String, String> informMap) {
        try {
            askService.inform(informMap);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }


    @PostMapping("/toggleLike")
    @ApiOperation(value = "点赞或不点赞", notes = "to great or not great， that is a question")
    @ApiImplicitParam(name = "queryMap", value = "查询条件askId、userId 这两个必须有", required = true, paramType = "body", dataType = "Map")
    public ResponseData<Answer> toggleLike(@RequestBody Map<String, String> queryMap) {
        try {
            askService.toggleLike(queryMap);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }



    @GetMapping("/informSuccess")
    @ApiOperation(value = "举报求助帖成功", notes = "举报成功某一个求助，需要求助帖主键askId")
    @ApiImplicitParam(name = "askId", value = "举报条件", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Comment> informSuccess(@RequestParam Integer askId) {
        try {
            askService.informSuccess(askId);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }



    @GetMapping("/informFail")
    @ApiOperation(value = "举报求助帖失败", notes = "举报失败某一个求助，需要求助帖主键askId")
    @ApiImplicitParam(name = "askId", value = "举报条件", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Comment> informFail(@RequestParam Integer askId) {
        try {
            askService.informFail(askId);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

}
